package com.rxmvp.baselibrary.utils;

import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;

import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

import static android.app.Activity.RESULT_CANCELED;

/**
 * 作者： huangRZ on 2017/2/14. 20:47
 * 邮箱：917647409@qq.com
 */

public class RxPermissionUtils {

    private static boolean IsBiggerAndroidM() {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M;
    }

    public static void requestPermissions(final Activity activity, String... permissions) {
        if (IsBiggerAndroidM()) {
            new RxPermissions(activity).request(permissions)
                    .subscribe(new Consumer<Boolean>() {
                        @Override
                        public void accept(Boolean granted) throws Exception {
                            if (!granted) {
                                showInfoDialog(activity, null);
                            }
                        }
                    });
        }
    }

    public static void requestEachPermissions(final Activity activity, String... permissions) {
        if (IsBiggerAndroidM()) {
            new RxPermissions(activity).requestEach(permissions)
                    .subscribe(new Consumer<Permission>() {
                        @Override
                        public void accept(Permission permission) throws Exception {
                            if (!permission.granted) {
                                showInfoDialog(activity, null);
                            }
                        }
                    });
        }
    }

    //请求相机权限
    public static void requestCameraPermission(final Activity activity){
        requestEachPermissions(activity, Manifest.permission.CAMERA);
    }

    //请求写SD卡权限
    public static void requestWriteStoragePermission(final Activity activity){
        requestEachPermissions(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    //请求读SD卡权限
    public static void requestReadStoragePermission(final Activity activity){
        requestEachPermissions(activity, Manifest.permission.READ_EXTERNAL_STORAGE);
    }

    /**
     * 当有必要权限被拒绝，弹出对话框提示，如果不设置，则退出
     *
     * @param activity 活动上下文
     */
    public static void showWarmingDialog(final Activity activity) {
        new AlertDialog.Builder(activity)
                .setTitle("提示")
                .setMessage("操作需要权限，请点击“设置”-“权限”-打开所需权限。")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + activity.getPackageName()));
                        activity.startActivity(intent);
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        activity.setResult(RESULT_CANCELED);
                        activity.finish();
                    }
                })
                .show();
    }

    public static void showInfoDialog(final Activity activity,
                                      DialogInterface.OnClickListener negativeListener) {
        new AlertDialog.Builder(activity)
                .setTitle("提示")
                .setMessage("为了能获得更好的体验，请点击“设置”-“权限管理”-“Custody”打开所需权限。")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
                        intent.setData(Uri.parse("package:" + activity.getPackageName()));
                        activity.startActivity(intent);
                    }
                })
                .setNegativeButton("取消", negativeListener)
                .show();
    }

}
